<?php

namespace app\admin\controller;

use wechat\src\WeChat;
use app\common\model\Qrcode;

/**
 * 微信菜二维码，
 */
class Wechatqrcode extends base
{

    protected $wechat;

    public function _initialize()
    {
        $this->wechat = (new WeChat);
        $this->wechat->valid();

    }

    /**
     * 二维码/统计
     *
     * @param string $type
     * @return \think\response\View
     */
    public function qrcode($type = 'list')
    {
        if ($this->request->isPost()) {
        } else {
            $qrModel = $qrModel =model('Qrcode');
            if ($type == 'list') {
                $data = $qrModel->order('id DESC')->paginate(10);
                $this->assign('data', $data);
            }
            if ($type == 'statistics') {
                $data = $qrModel->order('id DESC')->paginate(10);
                $this->assign('data', $data);
            }
            if ($type == 'friend') {
                $data = db('qrcode_data')->alias('a')->where(['a.scene_id' => input('scene_id'), 'a.type' => '1'])
                    ->join('__WUSER__ b', 'a.openid=b.openid')
                    ->order('a.create_time DESC')
                    ->field('a.*,b.nickname,b.headimgurl')
                    ->paginate(15);
                $this->assign('data', $data);
            }
            $this->assign('type', $type);
            return $this->fetch();
        }
    }

    /**
     * 增加二维码
     *
     * @return \think\response\View
     */
    public function qrcodeAdd()
    {
        if ($this->request->isPost()) {
            $qrModel = model('Qrcode');
            $IN = input();
            if ($qrModel->where(['scene_name' => $IN['name']])->find()) {
                ajaxMsg('0', '场景名称已经存在');
            }
            $data['keyword'] = $IN['keyword'];
            $data['scene_name'] = $IN['name'];
            $data['qr_type'] = $IN['qr_type'];
            $data['scene_str'] = $IN['scene_str'];
            $data['create_time'] = time();
            $data['scene_id'] = mt_rand(1, 1000000);
            if ($data['qr_type'] == 0) {//临时二维码
                $data['expire'] = $IN['time'];
                if ($data['expire'] == '') {
                    $data['expire'] = 1800;
                }

                $result = $this->wechat->instance('Qrcode')->createQrcode($data['scene_id'], $data['qr_type'], $data['expire']);
                $data['ticket'] = $result['ticket'];
                $data['qrcode_url'] = $this->wechat->instance('Qrcode')->getQrcode($result['ticket']);
                $data['short_url'] = $this->wechat->instance('Qrcode')->getQrshortUrl($data['qrcode_url']);
                $data['expire'] = $result['expire_seconds'];
                $data['url'] = $result['url'];
                if ($data['short_url'] == '') {
                    ajaxMsg('0', 'ErrMsg: 生成二维码短连接失败');
                }
                if (Qrcode::get(['scene_id' => $data['scene_id']])) {
                    $data['scene_id'] = mt_rand(1, 1000000);
                }
                if ($qrModel->save($data)) {
                    ajaxReturn(['url' => url('qrcode', ['type' => 'list'])], 1, '增加二维码成功');
                }
            }
            if ($data['qr_type'] == 2) {//永久二维码
                if (!$data['scene_str']) {
                    $data['scene_str'] = $data['scene_id'];
                }
                if (strlen($data['scene_str']) > 64) {
                    ajaxMsg('0', 'ErrMsg: 场景值字符小于64个字符');
                }
                $result = $this->wechat->instance('Qrcode')->createQrcode($data['scene_id'], $data['qr_type']);
                $data['ticket'] = $result['ticket'];
                $data['qrcode_url'] = $this->wechat->instance('Qrcode')->getQrcode($result['ticket']);
                $data['short_url'] = $this->wechat->instance('Qrcode')->getQrshortUrl($data['qrcode_url']);
                $data['url'] = $result['url'];
                if ($data['short_url'] == '') {
                    ajaxMsg('0', 'ErrMsg: 生成二维码短连接失败');
                }
                if (Qrcode::get(['scene_id' => $data['scene_id']])) {
                    $data['scene_id'] = mt_rand(1, 1000000);
                }
                $qrModel = model('Qrcode');
                if ($qrModel->save($data)) {
                    ajaxReturn(['url' => url('qrcode', ['type' => 'list'])], 1, '增加二维码成功');
                }

            }

        } else {
            return $this->fetch();
        }
    }

    public function delQrcode($id){
        if($this->request->isAjax()){
            Qrcode::destroy($id);
            db('qrcode_data')->where('qrcode_id','=',$id)->delete();
            ajaxMsg('1','删除成功');
        }
    }
}
